/// @author: cairuoyu
/// @homepage: http://cairuoyu.com
/// @github: https://github.com/cairuoyu/flutter_admin
/// @date: 2021/6/21
/// @version: 1.0
/// @description:

import 'package:flutter_admin/api/odoo/base_teamuser_odoo_api.dart';
import 'package:flutter_admin/models/odoo/base_team.dart';
import 'package:flutter_admin/models/odoo/base_teamuser.dart';
import 'package:flutter_admin/pages/common/cry/cry_transfer.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';

import 'station_user_select_list.dart';

class TeamUserSelect extends StatefulWidget {
  TeamUserSelect({Key? key, this.baseTeam}) : super(key: key);
  final BaseTeam? baseTeam;

  @override
  _TeamUserSelectState createState() => _TeamUserSelectState();
}

class _TeamUserSelectState extends State<TeamUserSelect> {
  final GlobalKey<TeamUserSelectListState> tableKey1 =
      GlobalKey<TeamUserSelectListState>();
  final GlobalKey<TeamUserSelectListState> tableKey2 =
      GlobalKey<TeamUserSelectListState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var table1 = TeamUserSelectList(
        key: tableKey1,
        title: '未配置人员',
        baseTeam: widget.baseTeam,);
    var table2 = TeamUserSelectList(
        key: tableKey2,
        title: '已配置人员',
        baseTeam: widget.baseTeam,
        isSelected: true);
    var transfer = CryTransfer(
      left: table1,
      right: table2,
      toRight: () async {
        List<BaseTeamuser> selectedList =
            tableKey1.currentState!.getSelectedList();
        if (selectedList.isEmpty) {
          CryUtils.message(S.of(context).selectUnselectedUsers);
          return;
        }
        List teamUserList = selectedList
            .map((e) => BaseTeamuser(
                    staffId: e.staffId,
                    teamId: widget.baseTeam!.id)
                .toOdooMap())
            .toList();
        await BaseTeamuserOdooApi.saveOrUpdate(teamUserList).then((ResponseBodyApi res){
          if(!res.success!){
            CryUtils.message(res.message!);
            return;
          }
          CryUtils.message(S.of(context).saved);
          tableKey1.currentState!.query();
          tableKey2.currentState!.query();
        });
      },
      toLeft: () async {
        List<BaseTeamuser> selectedList =
            tableKey2.currentState!.getSelectedList();
        if (selectedList.isEmpty) {
          CryUtils.message(S.of(context).selectSelectedUsers);
          return;
        }

        List ids = selectedList.map((v) {
          return int.parse(v.id!);
        }).toList();
        await BaseTeamuserOdooApi.removeByIds(ids).then((ResponseBodyApi res){
          if(!res.success!){
            CryUtils.message(res.message!);
            return;
          }
          CryUtils.message(S.of(context).saved);
          tableKey1.currentState!.query();
          tableKey2.currentState!.query();
        });
      },
    );

    var result = Scaffold(
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ScaffoldState? scaffold = Scaffold
                .maybeOf(context);
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;
            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('班组与人员配置'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [transfer],
        ),
      ),
    );
    return result;
  }
}
